import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_1911/pages/travel_page.dart';
import 'package:flutter_1911/pages/classification_page.dart';
import 'package:flutter_1911/pages/home_page.dart';
import 'package:flutter_1911/pages/vip_page.dart';

class IndexPage extends StatefulWidget {
  const IndexPage({Key? key}) : super(key: key);

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  PageController? _pageController;
  //设置底部导航栏样式
  final List<BottomNavigationBarItem> bottomTabs = [
    const BottomNavigationBarItem(icon: Icon(CupertinoIcons.home), label: '首页'),
    const BottomNavigationBarItem(icon: Icon(CupertinoIcons.search), label: '分类'),
    const BottomNavigationBarItem(icon: Icon(CupertinoIcons.camera), label: '旅拍'),
    const BottomNavigationBarItem(icon: Icon(CupertinoIcons.profile_circled), label: '会员中心'),
  ];
    //每一个页面添加到集合
  final List tabBodies = [
    HomePage(),
    ClassificationPage(),
    TravelPage(),
    VipPage()
  ];
    //初始位置
  int currentIndex = 0;
      //页面
  var currentPage;

  @override
  void initState() {
    //currentPage 等于点击的位置
    currentPage = tabBodies[currentIndex];
    _pageController = PageController()
      ..addListener(() {
        //判断是否是当前位置
        if (currentPage != _pageController?.page?.round()) {
          setState(() {
            currentPage = _pageController?.page?.round();
          });
        }
      });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //未选中下部导航栏的颜色
      backgroundColor: Color.fromRGBO(157, 52, 52, 1.0),
      bottomNavigationBar: BottomNavigationBar(
        //设置选中下部导航栏的颜色
        selectedItemColor: Colors.pinkAccent,
        type: BottomNavigationBarType.fixed,
        currentIndex: currentIndex,
        items: bottomTabs,
        //根据index切换下标
        onTap: (index) {
          setState(() {
            currentIndex = index;
            currentPage = tabBodies[currentIndex];
          });
        },
      ),
      //具体切换的页面
      body: IndexedStack(
        index: currentIndex,
        children: [
          HomePage(),
          ClassificationPage(),
          TravelPage(),
          VipPage()
        ],
      ),
    );
  }
}
